/*
  OpenEx for EverEdit
  -------------------
  Open file or link at caret
    Written by vinsa (vinsapro@gmail.com)
*/

(function(){
	function openUrl(url){
		var WshShell = new ActiveXObject("WScript.Shell");
		WshShell.Run( url );
	}

	function fileExists(filename) {
	    var fso = new ActiveXObject("Scripting.FileSystemObject");
	    return fso.FileExists(filename);
	}

	function openFile(filename){
		if (fileExists(filename)){
			App.OpenDoc(filename);
		}
	}

	function getDir(fullPath){
		var pos = fullPath.lastIndexOf("\\");
		return fullPath.substring(0, pos);
	}
	
	function openEx(str){
		if (str.indexOf(":\\") == 1){
			openFile(str);
		} else if (str.indexOf("http://") == 0 || str.indexOf("https://") == 0){
			openUrl(str);
		} else if (str.indexOf("@") > 0){
			openUrl("mailto:"+str);
		} else {
			var f = getDir(ActiveDoc.PathName) + "\\" + str;
			f = f.replace(/\//g,"\\");
			openFile(f);
		}
	}
	
	var caretWord = ActiveDoc.HasSel() ? ActiveDoc.SelText : ActiveDoc.GetWord(16|32) ;
	caretWord = caretWord.replace(/\"/g,"").replace(/'/g,"");
	openEx(caretWord);
})()